package com.zhangdi.dwperms.matcher;

import com.zhangdi.dwperms.commons.security.Encoder;
import com.zhangdi.dwperms.commons.security.Security;
import com.zhangdi.dwperms.commons.utils.SpringUtils;
import com.zhangdi.dwperms.dao.AccountDao;
import com.zhangdi.dwperms.entity.Account;
import com.zhangdi.dwperms.enums.LoginType;
import com.zhangdi.dwperms.token.AuthToken;
import com.zhangdi.dwperms.token.MSToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * Created by zhangdi on 2020/08/01 14:42:45.
 */
public class JwtCredentialsMatcher extends HashedCredentialsMatcher {
    @Resource
    AccountDao accountDao;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        accountDao = SpringUtils.getBean("accountDao");
        Account account = accountDao.findByUsername((String) authenticationToken.getPrincipal());
        if(null == account){
            return false;
        }
//        account.getPassword(),
//                ByteSource.Util.bytes(Encoder.decode(account.getSalt()))

//        account.setPassword(security.encrypt(params.getPassword()));
//        account.setSalt(security.getSalt());

        Object tokenHashedCredentials = this.hashProvidedCredentials(authenticationToken, authenticationInfo);
        Object accountCredentials = this.getCredentials(authenticationInfo);
        if(this.equals(tokenHashedCredentials, accountCredentials)){
            return true;
        }

        return false;
    }
}
